package public

import (
	"crypto/sha256"
	"fmt"
	"math/rand"
	"time"
)

func GenSaltPassword(salt, password string) string {
	sha1 := sha256.New()
	sha1.Write([]byte(password))
	str1 := fmt.Sprintf("%x", sha1.Sum(nil))

	sha2 := sha256.New()
	sha2.Write([]byte(str1 + salt))
	return fmt.Sprintf("%x", sha2.Sum(nil))
}

var defaultLetters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
func RandomString(n int, allowedChars ...[]rune) string {
	rand.Seed(time.Now().UnixNano())
	var letters []rune

	if len(allowedChars) == 0 {
		letters = defaultLetters
	} else {
		letters = allowedChars[0]
	}

	b := make([]rune, n)
	for i := range b {
		b[i] = letters[rand.Intn(len(letters))]
	}

	return string(b)
}
